<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesResources;
use Cache;
use App\Job;
use App\Category;
use PhpParser\Node\Expr\Array_;

class Controller extends BaseController
{
    use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;

    public function __construct()
    {
        if(Cache::has('nav_list')){
            $nav_list = Cache::get('nav_list');
        } else {
            $navs = Category::orderBy('parentid', 'ASC')->get();
            $nav_list = [];
            foreach ($navs as $nav) {
                if (1 == $nav->rank) {
                    $nav_list[$nav->categoryid] = [];
                    $nav_list[$nav->categoryid]['name'] = $nav->name;
                    $nav_list[$nav->categoryid]['data'] = [];
                    $nav_list[$nav->categoryid]['content'] = $nav->content;
                } else {
                    array_push($nav_list[$nav->parentid]['data'], $nav);
                }
            }
            Cache::forever('nav_list',$nav_list);
        }
        $jump = config('site.jump');
        view()->share('nav_list', $nav_list);
        view()->share('jump', $jump);
    }
}
